﻿using SHomeWorkshop.LunarPresentation.Dialogs;

namespace SHomeWorkshop.LunarPresentation.Commands
{
    public partial class Command
    {
        public static void EditMarkText(Expressions.Expression curEx)
        {
            Expressions.MarkText mt = curEx as Expressions.MarkText;

            MarkTextEditor meditor = new MarkTextEditor()
            {
                TopMarkText = mt.TopMarkText,
                BottomMarkText = mt.BottomMarkText,
                Owner = curEx.Editor.ParentWindow
            };

            if (meditor.ShowDialog() == true)
            {
                #region 准备“撤销”与“重做”数据。
                ModifingItem mi = new ModifingItem();
                Action actionOfTopMarkText = new Action(mt.ExID,
                    mt.ParentPanel.BasePanel.Children.IndexOf(mt),
                    typeof(Expressions.MarkText).Name,
                    "TopMarkText", mt.TopMarkText, meditor.TopMarkText);
                mi.Add(actionOfTopMarkText);

                Action actionOfBottomMarkText = new Action(mt.ExID,
                    mt.ParentPanel.BasePanel.Children.IndexOf(mt),
                    typeof(Expressions.MarkText).Name,
                    "BottomMarkText", mt.BottomMarkText, meditor.BottomMarkText);
                mi.Add(actionOfBottomMarkText);
                #endregion

                mt.TopMarkText = meditor.TopMarkText;
                mt.BottomMarkText = meditor.BottomMarkText;

                //加入“撤销”列表。
                mt.Editor.Do(mi);
            }
        }

        /// <summary>
        /// 交换上下标式的上、下标文本。
        /// </summary>
        public static void SwitchMarks()
        {
            if (Globals.selectedEditor == null || Globals.selectedEditor.SelectedExpression == null) return;
            if (Globals.selectedEditor.SelectedExpression is Expressions.MarkText == false) return;

            Expressions.MarkText mt = Globals.selectedEditor.SelectedExpression as Expressions.MarkText;

            ModifingItem mi = new ModifingItem() { OldSelectedExID = mt.ExID, NewSelectedExID = mt.ExID };

            string tmp = mt.TopMarkText;

            Action actTopMark = new Action(mt.ExID, -1, mt.GetType().Name, "TopMarkText", mt.TopMarkText,
                mt.BottomMarkText);

            mt.TopMarkText = mt.BottomMarkText;

            Action actBottomMark = new Action(mt.ExID, -1, mt.GetType().Name, "BottomMarkText", mt.BottomMarkText,
                tmp);

            mt.BottomMarkText = tmp;

            mi.Add(actTopMark);
            mi.Add(actBottomMark);

            mt.Editor.Do(mi);
            mt.Editor.IsModified = true;
        }
    }
}
